from flask_restful import Resource, fields, marshal_with, reqparse
from flask import Flask, request
from baidu import run_xueshu_page

from flask_cors import CORS, cross_origin
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 设置选项：无窗口后台运行
chrome_options = Options()
chrome_options.add_argument('--headless')
#
# 取得driver
browser = webdriver.Chrome("/usr/local/bin/chromedriver",chrome_options=chrome_options)
parser = reqparse.RequestParser()


class Search_key_Word(Resource):
    def get(self):
        print()
        # 获取get ? 参数
        wd = request.args.get("wd")
        pageNum = request.args.get("pageNum")
        if(not pageNum):
            pageNum = 10
        print(pageNum)
        print(int(pageNum) <= 0 or type(int(pageNum)) != int)
        if(int(pageNum) <= 0 or type(int(pageNum)) != int):
            return {
                "status": "errors",
                "msg": "参数格式不正确,页数为正整数"
            }
        try:
            return run_xueshu_page(wd=wd, pageName=pageNum,browser=browser)
        except:
            return {
                "status":"errors",
                "msg":"爬取数据失败"
            }
